package com.youlu.campus.website.controller;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.website.entity.CompanyDynamicType;
import com.youlu.campus.website.entity.CompanyDynamicTypePageQuery;
import com.youlu.campus.website.service.CompanyDynamicTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 公司动态类型
 */
@RestController
@RequestMapping(value = "/website/company/dynamic/type")
public class CompanyDynamicTypeController {

    @Autowired
    private CompanyDynamicTypeService companyDynamicTypeService;

    /**
     * 新增/修改动态类型
     *
     * @param type
     * @return
     */
    @PostMapping("/save")
    public QResult<CompanyDynamicType> save(@RequestBody CompanyDynamicType type) {
        if (StringUtils.isAnyBlank(type.getType(), type.getName())) {
            return QResult.fail("参数错误");
        }
        return QResult.success(companyDynamicTypeService.save(type));
    }

    /**
     * 根据 ID 查询动态类型
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    public QResult<CompanyDynamicType> findById(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return QResult.fail("参数错误");
        }
        return QResult.success(companyDynamicTypeService.findById(id));

    }

    /**
     * 查询所有动态类型
     *
     * @return
     */
    @GetMapping("/find-all")
    public QResult<List<CompanyDynamicType>> getAllTypes() {
        return QResult.success(companyDynamicTypeService.getAllTypes());
    }

    /**
     * 分页查询动态类型
     *
     * @return
     */
    @PostMapping("/find-page")
    public QResult<Page<CompanyDynamicType>> findByPage(@RequestBody CompanyDynamicTypePageQuery pageQuery) {
        return QResult.success(companyDynamicTypeService.findByPage(pageQuery));
    }

    /**
     * 根据 ID 删除动态类型
     *
     * @param dynamicType
     */
    @PostMapping("/delete")
    public QResult delete(@RequestBody CompanyDynamicType dynamicType) {
        if (StringUtils.isBlank(dynamicType.getId())) {
            return QResult.fail("参数错误");
        }
        companyDynamicTypeService.delete(dynamicType.getId());
        return QResult.success();
    }

}